package com.tzjt.listview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StudentDao {
	private SqlHelper sqlHelper;
	private String sql;

	public StudentDao(Context context) {
		sqlHelper = new SqlHelper(context);
	}
	
	 public void insertData(Student stu) {
		  sql = "insert into student (name,age,sex)values(?,?,?)";
		  System.out.println("name "+stu.getName());
		  SQLiteDatabase db = sqlHelper.getWritableDatabase();
		  db.execSQL(sql, new Object[] { stu.getName(), stu.getAge(),stu.getSex() });
		 }
	 
	 public List<Student> getStudentList() {
		// TODO Auto-generated method stub
		 List<Student> stus = new ArrayList<Student>();
		 sql="select * from student order by id desc";
		 SQLiteDatabase db = sqlHelper.getWritableDatabase();
		 Cursor cursor = db.rawQuery(sql, null);

		 while (cursor.moveToNext()) {
			 Student student = new Student();
			 student.setId(cursor.getInt(0)) ; //获取第一列的值,第一列的索引从0开始
			 student.setName(cursor.getString(1)) ;//获取第二列的值
			 student.setAge(cursor.getInt(2));//获取第三列的值
			 student.setSex(cursor.getString(3));
			 stus.add(student);
			}
		 cursor.close();
		 return stus;
	}
	 
	 public void delStudentById(int stuid) {
		 
		// TODO Auto-generated method stub

	}
}
